<?php
class View_Bug_Manager_View extends CMF_Krypton_Core_View
{
	/**
	 *	Contructs HTML Output.
	 *	@access		public
	 *	@return		string
	 */
	public function buildContent()
	{
		$config		= $this->registry->get( 'config' );
		$request	= $this->registry->get( 'request' );
		$words		= $this->words['bug']['view'];

		if( $bug_id	= $request->get( 'bug_id' ) )
		{
			$bug	= new Bug( $bug_id );
			$data	= $bug->getData( TRUE );
			if( count( $data ) )
			{
				$link_bug_id		= $this->html->Link( "?link=bug&bug_id=".$data['bug_id'], $words['link_bug'] );
				$user			= new User( $data['author'] );

				$data['author']	= $user->getUsername();
				$data['description']	= nl2br( $data['description'] );
				$data['bug_id']	= $data['bug_id']." => ".$link_bug_id;
				$data['priority']	= $this->words['main']['priorities'][$data['priority']];
				$data['status']		= $this->words['main']['states'][$data['status']];
				$data['type']		= $this->words['main']['types'][$data['type']];
				$data['created']	= $this->tc->convertToHuman( $data['created'], $config['layout']['format_timestamp'] );
				$data['modified']	= $this->tc->convertToHuman( $data['modified'], $config['layout']['format_timestamp'] );
				$project		= new Project( $data['project'] );
				$project		= $project->getData( TRUE );

				$server = new Server( $data['server' ] );
				$server		= $server->getData( TRUE );

				$data['project']	= $server['title']."->".$project['title'];

				//  --  DEVELOPER ASSIGNMENTS  --  //
				$assign			= new Assignment();
				$assign->focusForeign( "bug_id", $bug_id );
				$devs			= $assign->getData( false );
				if( count( $devs ) )
				{
					$dev_list			= array();
					foreach( $devs as $dev )
					{
						$user		= new User( $dev['user_id'] );
						$user		= $user->getData( TRUE );
						$dev_list[]	= $user['username'];
					}
					$data['developers']	= implode( ", ", $dev_list );
				}
				else
					$data['developers']	= $words['no_developers'];

				//  --  COMPONENTS  --  //
				$ui	= $this->buildForm( 'bug', 'viewBug', 'bug', 'view' , $data );

				//  --  ATTACHMENTS  --  //
				$list			= array();
				$attachments	= new Attachment();
				$attachments->focusForeign( "bug_id", $bug_id );
				$data		= $attachments->getData( FALSE );
				if( count( $data ) )
				{
					foreach( $data as $attachment )
					{
						$uri		= $config['paths']['attachments'].$attachment['filename'];
						$title	= $attachment['title'];
						if( $attachment['description'] )
							$title	= $this->html->Acronym( $attachment['title'], $attachment['description'] );
						$list[]	= "<li>".$this->html->Link( $uri, $title, '', '_blank' )."</li>";
					}
					$attachments	= implode( "", $list );
				}
				else
					$attachments	= $words['no_attachments'];

				$ui['label_attachments']	= $this->html->Label( '', $words['attachments'] );
				$ui['field_attachments']	= $this->html->Field( '', "<ul style='list-style:none'>".$attachments."</ul>" );

				//  --  COMMENTS  --  //
				$list			= array();
				$_comments	= new Comment();
				$_comments->focusForeign( "bug_id", $bug_id );
				$comments	= $_comments->getData( FALSE );
				foreach( $comments as $comment )
				{
					$user	= new User( $comment['author'] );
					$author	= $user->getUsername();
					$text	= nl2br( $comment['comment'] );
					$text	= $this->includeBugLink( $text );
					$status	= $comment['status'] ? "<br/><b>[".$this->words['main']['states'][$comment['status']]."]</b>" : "";
					$label	= $author."<br/>".date( "d.m.y H:i:s", $comment['timestamp'] ).$status;
					$list[]	= "<tr>".$this->html->Label( '', $label ).$this->html->Field( '', $text )."</tr>";
				}
				$ui['comments']	= implode( "", $list );

				$button_comment	= $this->html->Button( 'addComment', $words['button_comment'] );
				$ui['field_button']	= $this->html->Field( '', $button_comment );


				$input_comment	= $this->html->TextArea( 'add_comment', '', 'mm' );
				$ui['label_comment']	= $this->html->Label( 'add_comment', $words['comment'] );
				$ui['field_comment']	= $this->html->Field( 'add_comment', $input_comment );

				//  --  BUG PAGING  --  //
				$ui['button_previous']	= "<button class='buttiny' disabled='disabled'><<</button>";
				$ui['button_next']		= "<button class='buttiny' disabled='disabled'>>></button>";

				if( $prev = $bug->getPreviousBug( $bug_id ) )
					$ui['button_previous']		= "<button class='buttiny' onClick=\"document.location.href='"."?link=".$request->get( 'link' )."&bug_id='+".($prev).";\"><<</button>";
				if( $next = $bug->getNextBug( $bug_id ) )
					$ui['button_next']			= "<button class='buttiny' onClick=\"document.location.href='"."?link=".$request->get( 'link' )."&bug_id='+".($next).";\">>></button>";

				$ui['input_bug_id']		= $this->html->Input( 'bug_id', $bug_id, 'inbit' );
				$ui['button_bug_id']	= $this->html->Button( 'setBug', '>', 'butbit' );
				$ui['button_bug_id']	= "<button class='butbit' onClick=\"document.location.href='"."?link=".$request->get( 'link' )."&bug_id='+document.getElementById('bug_id').value;\">></button>";

				$ui['form']		= $this->html->Form( 'viewBug', "?link=".$request->get( 'link' )."&bug_id=".$request->get( 'bug_id' ) );
				$ui['colgroup']		= $this->html->ColumnGroup( "30%", "70%" );
				$ui['caption']		= $this->html->TableCaption( $words['caption'] );
				$ui['heading']		= $words['heading'];

				$content	= $this->loadTemplate( 'bug.view', $ui );
				return $content;
			}
			else
				$this->messenger->noteError( "Bug is not existing." );
		}
		else
			$this->messenger->noteError( $this->words['bug']['msg']['error1'] );
	}
}
?>